package io.renren.modules.sys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.modules.sys.dao.IntelReplenDao;
import io.renren.modules.sys.entity.GoodshelvesEntity;
import io.renren.modules.sys.entity.IntelReplenEntity;
import io.renren.modules.sys.entity.ProductshelvesstcokEntity;
import io.renren.modules.sys.entity.IntelReplenVOEntity;
import io.renren.modules.sys.service.GoodshelvesService;
import io.renren.modules.sys.service.IntelReplenService;
import io.renren.modules.sys.service.ProductshelvesstcokService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.math.BigDecimal;


@Service("intelreplenService")
public class IntelReplenServiceImpl extends ServiceImpl<IntelReplenDao, IntelReplenEntity> implements IntelReplenService {

    @Autowired
    private GoodshelvesService goodshelvesService;

    @Autowired
    private ProductshelvesstcokService productshelvesstcokService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name = null;
        if( params.get("name")!=null&&!params.get("name").equals("")){
            name = params.get("name").toString();
        }

        Integer shelvesid = 0;
        if( params.get("shelvesid")!=null&&!params.get("shelvesid").equals("")){
            shelvesid = Integer.parseInt(params.get("shelvesid").toString());
        }

        String categoryid = null;
        if( params.get("categoryid")!=null&&!params.get("categoryid").equals("")){
            categoryid = params.get("categoryid").toString();
        }

        int pageSize = Integer.parseInt(params.get("limit").toString());  //每页记录数
        int currPage = Integer.parseInt(params.get("page").toString());  //当前页数


        int count = baseMapper.queryListcount(name,shelvesid,categoryid);
        List<IntelReplenEntity> list = baseMapper.queryListPage(name,shelvesid,categoryid,(currPage-1)*pageSize,currPage*pageSize);
        List<GoodshelvesEntity> gslist = goodshelvesService.list(new QueryWrapper<GoodshelvesEntity>());
        if(list!=null && list.size()>0){
            for(IntelReplenEntity ir:list){
                int jybh = 0;
                if(gslist!=null && gslist.size()>0) {


                    for (GoodshelvesEntity gs : gslist) {
                        ProductshelvesstcokEntity stocklist = productshelvesstcokService.getByProductIdAndShelvesId(ir.getId(),gs.getId());
                        if(stocklist!=null){
                            int stock = 0;
                            if(ir.getUpperLimit()>stocklist.getStock()){
                                stock = ir.getUpperLimit() -stocklist.getStock();
                            }
                            if(stock<0){
                                stock = 0;
                            }
                            jybh +=stock;
                        }

                    }
                }


                /*if(stocklist!=null && stocklist.size()>0){
                    for(ProductshelvesstcokEntity pse:stocklist){
                        stock += pse.getStock();
                    }
                }
                jybh = ir.getUpperLimit() -stock;
                if(jybh<0){
                    jybh = 0;
                }*/
                ir.setBuycount(jybh);
                ir.setTotalcost(ir.getAvgcost().multiply(new BigDecimal(jybh)));
            }
        }
        return new PageUtils(list,count,pageSize,currPage);
    }

    @Override
    public List<IntelReplenEntity> queryList(String name,String categoryid,Integer shelvesid) {

        List<IntelReplenEntity> lists = new ArrayList<IntelReplenEntity>();
        List<IntelReplenEntity> list = baseMapper.queryList(name,shelvesid,categoryid);
        List<GoodshelvesEntity> gslist = goodshelvesService.list(new QueryWrapper<GoodshelvesEntity>());
        if(list!=null && list.size()>0){
            for(IntelReplenEntity ir:list){
                /*List<ProductshelvesstcokEntity> stocklist = baseMapper.queryProductStockByPid(ir.getId());
                int jybh = 0;
                int stock = 0;
                if(stocklist!=null && stocklist.size()>0){
                    for(ProductshelvesstcokEntity pse:stocklist){
                        stock += pse.getStock();
                    }
                }
                jybh = ir.getUpperLimit() -stock;
                if(jybh<0){
                    jybh = 0;
                }*/

                int jybh = 0;
                if(gslist!=null && gslist.size()>0) {

                    for (GoodshelvesEntity gs : gslist) {
                        ProductshelvesstcokEntity stocklist = productshelvesstcokService.getByProductIdAndShelvesId(ir.getId(),gs.getId());
                        if(stocklist!=null){
                            int stock = 0;
                            if(ir.getUpperLimit()>stocklist.getStock()){
                                stock = ir.getUpperLimit() -stocklist.getStock();
                            }
                            if(stock<0){
                                stock = 0;
                            }
                            jybh +=stock;
                        }

                    }
                }

                ir.setBuycount(jybh);
                ir.setTotalcost(ir.getAvgcost().multiply(new BigDecimal(jybh)));
                if(ir.getBuycount()>0){
                    lists.add(ir);
                }
            }
        }
        return lists;
    }

    @Override
    public IntelReplenVOEntity getStatistics(Map<String, Object> params){
        String name = null;
        if( params.get("name")!=null&&!params.get("name").equals("")){
            name = params.get("name").toString();
        }

        Integer shelvesid = 0;
        if( params.get("shelvesid")!=null&&!params.get("shelvesid").equals("")){
            shelvesid = Integer.parseInt(params.get("shelvesid").toString());
        }

        String categoryid = null;
        if( params.get("categoryid")!=null&&!params.get("categoryid").equals("")){
            categoryid = params.get("categoryid").toString();
        }

        List<IntelReplenEntity> list = baseMapper.getStatistics(name,shelvesid,categoryid);
        int buycount = 0;
        BigDecimal totalcost = new BigDecimal(0);
        if(list!=null && list.size()>0){
            for(IntelReplenEntity ir:list){
                List<ProductshelvesstcokEntity> stocklist = baseMapper.queryProductStockByPid(ir.getId());
                int jybh = 0;
                int stock = 0;
                if(stocklist!=null && stocklist.size()>0){
                    for(ProductshelvesstcokEntity pse:stocklist){
                        stock += pse.getStock();
                    }
                }
                jybh = ir.getUpperLimit() -stock;
                if(jybh<0){
                    jybh = 0;
                }
                buycount += jybh;
                totalcost = totalcost.add(ir.getAvgcost().multiply(new BigDecimal(jybh)));
            }
        }
        IntelReplenVOEntity entity = new IntelReplenVOEntity();
        entity.setBuycount(buycount);
        entity.setCount(list.size());
        entity.setTotalcost(totalcost);
        return entity;
    }

}
